import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
Vue.use(VueRouter)
function hasUrl(path, next) {
  let result = store.state.User.menus_url.find((item) => item == path);
  if (result) {
    next()
  } else {
    next('/home')
  }
}
export const indexRouter = [
  {
    path: '/menu',
    component: () => import('../views/menu/menu.vue'),
    meta: {
      title: '菜单管理'
    },
    beforeEnter: (to, from, next) => {
      hasUrl("/menu", next);
    },
  },
  {
    path: '/role',
    component: () => import('../views/role/role.vue'),
    meta: {
      title: '角色管理'
    },
    beforeEnter: (to, from, next) => {
      hasUrl("/role", next);
    },
  },
  {
    path: '/manger',
    component: () => import('../views/manger/manger.vue'),
    meta: {
      title: '管理员管理'
    },
    beforeEnter: (to, from, next) => {
      hasUrl("/manger", next);
    },
  },
  {
    path: '/cate',
    component: () => import('../views/cate/cate.vue'),
    meta: {
      title: '商品分类'
    },
    beforeEnter: (to, from, next) => {
      hasUrl("/cate", next);
    },
  },
  {
    path: '/specas',
    component: () => import('../views/specas/specas.vue'),
    meta: {
      title: '商品规格'
    },
    beforeEnter: (to, from, next) => {
      hasUrl("/specas", next);
    },
  },
  {
    path: '/shopmanger',
    component: () => import('../views/shopmanger/shopmanger.vue'),
    meta: {
      title: '商品管理'
    },
    beforeEnter: (to, from, next) => {
      hasUrl("/shopmanger", next);
    },
  },
  {
    path: '/member',
    component: () => import('../views/member/member.vue'),
    meta: {
      title: '会员列表'
    },
    beforeEnter: (to, from, next) => {
      hasUrl("/member", next);
    },
  },
  {
    path: '/banner',
    component: () => import('../views/banner/banner.vue'),
    meta: {
      title: '轮播图管理'
    },
    beforeEnter: (to, from, next) => {
      hasUrl("/banner", next);
    },
  },
  {
    path: '/seck',
    component: () => import('../views/seck/seck.vue'),
    meta: {
      title: '限时秒杀管理'
    },
    beforeEnter: (to, from, next) => {
      hasUrl("/seck", next);
    },
  },

]
const routes = [
  {
    path: '/login',
    component: () => import('../pages/login.vue')
  },
  {
    path: '/index',
    component: () => import('../pages/index.vue'),
    children: [
      {
        path: '/home',
        component: () => import('../views/home.vue')
      },
      ...indexRouter,
      {
        path: '',
        redirect: '/home'
      }
    ]
  },
  {
    path: '*',
    redirect: '/login'
  }
]

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next)=>{
  if(to.path=='/login'){
    next()
    return;
  }
  if(store.state.User){
    next();
    return
  }
  next('/login');
})

export default router
